#include "widget_human.h"
#include "gui.h"



map_square_buy::map_square_buy( QGraphicsItem* parent, QGraphicsScene* scene ): QGraphicsPixmapItem( parent, scene )
{
};



map_bonus_buy::map_bonus_buy( bonus* b, QGraphicsItem* parent, QGraphicsScene* scene ): QGraphicsPixmapItem( parent, scene )
{
	bon = b;
	this->setPixmap( *game_gui::get_instance()->graphics->bonus_pixmaps[ b->get_name() ] );
	this->setZValue( 7 );
	this->setShapeMode( QGraphicsPixmapItem::BoundingRectShape );
};



map_units_square::map_units_square( unsigned int id, QGraphicsItem* parent, QGraphicsScene* scene ): QGraphicsPixmapItem( parent, scene )
{
	game_gui* ggui = game_gui::get_instance();
	this->setPixmap( *ggui->graphics->begin_pixmaps[ ggui->graphics->get_color( id ) ] );
	this->setZValue( 5 );
	this->setShapeMode( QGraphicsPixmapItem::BoundingRectShape );
};



void map_units_square::mousePressEvent( QGraphicsSceneMouseEvent* event )
{
	game_gui* ggui = game_gui::get_instance();
	if ( event->button() == Qt::LeftButton )
	{
		if ( ggui->g->buy_w->human_w->bought_unit != -1 )
		{
			unsigned int x = ( unsigned int )( this->pos().x() / parameters::get_instance()->gui_parameters[ "ICON_W" ] );
			unsigned int y = ( unsigned int )( this->pos().y() / parameters::get_instance()->gui_parameters[ "ICON_H" ] );		
			//place unit on own map_units_square
			ggui->g->buy_w->human_w->forward_command( ggui->g->buy_w->human_w->current_player, game::create_command( "BUY", ggui->g->buy_w->human_w->bought_unit, x, y ) );
		}
	}
};



map_unit_buy::map_unit_buy( unit* un, QGraphicsItem* parent, QGraphicsScene* scene ): QGraphicsPixmapItem( parent, scene )
{
	game_gui* ggui = game_gui::get_instance();
	u = un;	
	this->setPixmap( *ggui->graphics->units_pixmaps[ un->get_name() ][ ggui->graphics->get_color( un->get_player()->get_id() ) ].first );
	this->setZValue( 10 );
	this->setShapeMode( QGraphicsPixmapItem::BoundingRectShape );
};



void map_unit_buy::mousePressEvent( QGraphicsSceneMouseEvent* event )
{
	game_gui* ggui = game_gui::get_instance();
	if ( event->button() == Qt::RightButton )
	{
		//sell only owned units
		if ( u->get_player() == ggui->g->buy_w->human_w->current_player )
		{
			ggui->g->buy_w->human_w->forward_command( ggui->g->buy_w->human_w->current_player, game::create_command( "SEL", u->get_id() ) );
		}
	}
};




unit_buy::unit_buy( unit *un, QWidget *parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{
	u = un;

	QHBoxLayout* layout = new QHBoxLayout( this );

	QLabel* name_l = new QLabel( this );
	name_l->setTextFormat( Qt::RichText );
	ostringstream sout;
	sout << "<font size=5>" << un->get_name() << ":</font>";
	name_l->setMinimumWidth( 120 );
	name_l->setText( sout.str().c_str() );
	layout->addWidget( name_l );
	sout.str( "" );

	QLabel* price_l = new QLabel( this );
	price_l->setTextFormat( Qt::RichText );
	sout << "<font size=4>" << un->get_price() << "</font>";
	price_l->setMinimumWidth( 70 );
	price_l->setText( sout.str().c_str() );
	layout->addWidget( price_l );
	sout.str( "" );

	QLabel* range_l = new QLabel( this );
	range_l->setTextFormat( Qt::RichText );
	sout << "<font size=4>" << un->get_range() << "</font>";
	range_l->setMinimumWidth( 70 );
	range_l->setText( sout.str().c_str() );
	layout->addWidget( range_l );
	sout.str( "" );

	QLabel* health_l = new QLabel( this );
	health_l->setTextFormat( Qt::RichText );
	sout << "<font size=4>" << un->get_health() << "</font>";
	health_l->setMinimumWidth( 70 );
	health_l->setText( sout.str().c_str() );
	layout->addWidget( health_l );
	sout.str( "" );

	QLabel* shield_l = new QLabel( this );
	shield_l->setTextFormat( Qt::RichText );
	sout << "<font size=4>" << un->get_shield() << "</font>";
	shield_l->setMinimumWidth( 70 );
	shield_l->setText( sout.str().c_str() );
	layout->addWidget( shield_l );
	sout.str( "" );

	QLabel* weapon_l = new QLabel( this );
	weapon_l->setTextFormat( Qt::RichText );
	sout << "<font size=4>" << un->get_weapon() << "</font>";
	weapon_l->setMinimumWidth( 70 );
	weapon_l->setText( sout.str().c_str() );
	layout->addWidget( weapon_l );
	sout.str( "" );

	QLabel* cost_move_l = new QLabel( this );
	cost_move_l->setTextFormat( Qt::RichText );
	sout << "<font size=4>" << un->get_cost_move() << "</font>";
	cost_move_l->setMinimumWidth( 70 );
	cost_move_l->setText( sout.str().c_str() );
	layout->addWidget( cost_move_l );
	sout.str( "" );

	QLabel* cost_fire_l = new QLabel( this );
	cost_fire_l->setTextFormat( Qt::RichText );
	sout << "<font size=4>" << un->get_cost_fire() << "</font>";
	cost_fire_l->setMinimumWidth( 70 );
	cost_fire_l->setText( sout.str().c_str() );
	layout->addWidget( cost_fire_l );
	sout.str( "" );

	buy_b = new QPushButton( "Buy", this );
	buy_b->setFixedWidth( 100 );
	layout->addWidget( buy_b );
	connect( buy_b, SIGNAL( clicked() ), this, SLOT( buy_unit() ) );

	this->setLayout( layout );
};



void unit_buy::buy_unit()
{
	game_gui::get_instance()->g->buy_w->human_w->bought_unit = prototypes::get_instance()->units_name_pos( u->get_name() );
};



human_widget::human_widget( player* pl, QPushButton* nb, QWidget* parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{
	connect( this, SIGNAL( send_command( player*, const string& ) ), game::get_instance(), SLOT( command_received( player*, const string& ) ) );
	next_b = nb;
	next_b->setEnabled( true );
	bought_unit = -1;
	current_player = game::get_instance()->get_current_player();
	map_array = new array2d< map_square_buy* >( mapa::get_instance()->get_size_x(), mapa::get_instance()->get_size_y() );



	QVBoxLayout* layout = new QVBoxLayout( this );
	


	QWidget* title_w = new QWidget( this );
	QHBoxLayout* title_w_l = new QHBoxLayout( title_w );


	QLabel* title_l = new QLabel( title_w );
	title_l->setTextFormat( Qt::RichText );
	ostringstream sout;
	sout << "<font size=6>" << pl->get_name() << " :</font>";
	title_l->setText( sout.str().c_str() );
	title_w_l->addWidget( title_l );
	sout.str( "" );


	money_l = new QLabel( title_w );
	money_l->setTextFormat( Qt::RichText );
	set_label_money();
	title_w_l->addWidget( money_l );


	title_w->setLayout( title_w_l );
	layout->addWidget( title_w );




	QWidget* map_buy_w = new QWidget( this );
	QHBoxLayout* map_buy_w_l = new QHBoxLayout( map_buy_w );


	parameters* param = parameters::get_instance();

	QGraphicsView *gv = new QGraphicsView( map_buy_w );
	gv->setBackgroundBrush( QBrush( QColor( 105, 207, 105 ), Qt::Dense6Pattern ) );
	gv->setMaximumSize( mapa::get_instance()->get_size_x() * param->gui_parameters[ "ICON_W" ] + 20, mapa::get_instance()->get_size_y() * param->gui_parameters[ "ICON_H" ] + 20 );
	gs = new QGraphicsScene(0, 0, mapa::get_instance()->get_size_x() * param->gui_parameters[ "ICON_W" ], mapa::get_instance()->get_size_y() * param->gui_parameters[ "ICON_H" ], gv);
	gs->setBackgroundBrush( *( game_gui::get_instance()->graphics->map_background ) );
	create_map();

	gv->setScene( gs );
	map_buy_w_l->addWidget( gv );


	bought_units_w = new QWidget( map_buy_w );
	bought_units_w_l = new QVBoxLayout( bought_units_w );
	bought_units_w_l->setSpacing( 20 );

	QLabel* bought_title_l = new QLabel( bought_units_w );
	bought_title_l->setTextFormat( Qt::RichText );
	sout << "<font size=5>Bought units</font>";
	bought_title_l->setText( sout.str().c_str() );
	bought_units_w_l->addWidget( bought_title_l );

	bought_units_w_l->addSpacing( 20 );

	bought_units_w_l->addStretch();

	bought_units_w->setLayout( bought_units_w_l );
	map_buy_w_l->addWidget( bought_units_w );


	map_buy_w->setLayout( map_buy_w_l );
	layout->addWidget( map_buy_w );

	


	QWidget* info_title_w = new QWidget( this );
	QHBoxLayout* info_title_w_l = new QHBoxLayout( info_title_w );

	QLabel* name_l = new QLabel( info_title_w );
	name_l->setTextFormat( Qt::RichText );
	name_l->setMinimumWidth( 120 );
	name_l->setText( "<font size=5>Name</font>" );
	info_title_w_l->addWidget( name_l );

	QLabel* price_l = new QLabel( info_title_w );
	price_l->setTextFormat( Qt::RichText );
	price_l->setText( "<font size=4>Price</font>" );
	price_l->setMinimumWidth( 70 );
	info_title_w_l->addWidget( price_l );

	QLabel* range_l = new QLabel( info_title_w );
	range_l->setTextFormat( Qt::RichText );
	range_l->setText( "<font size=4>Range</font>" );
	range_l->setMinimumWidth( 70 );
	info_title_w_l->addWidget( range_l );

	QLabel* health_l = new QLabel( info_title_w );
	health_l->setTextFormat( Qt::RichText );
	health_l->setText( "<font size=4>Health</font>" );
	health_l->setMinimumWidth( 70 );
	info_title_w_l->addWidget( health_l );

	QLabel* shield_l = new QLabel( info_title_w );
	shield_l->setTextFormat( Qt::RichText );
	shield_l->setText( "<font size = 4>Shield</font>" );
	shield_l->setMinimumWidth( 70 );
	info_title_w_l->addWidget( shield_l );

	QLabel* weapon_l = new QLabel( info_title_w );
	weapon_l->setTextFormat( Qt::RichText );
	weapon_l->setText( "<font size = 4>Weapon</font>" );
	weapon_l->setMinimumWidth( 70 );
	info_title_w_l->addWidget( weapon_l );

	QLabel* cost_move_l = new QLabel( info_title_w );
	cost_move_l->setTextFormat( Qt::RichText );
	cost_move_l->setText( "<font size = 4>Cost move</font>" );
	cost_move_l->setMinimumWidth( 70 );
	info_title_w_l->addWidget( cost_move_l );

	QLabel* cost_fire_l = new QLabel( info_title_w );
	cost_fire_l->setTextFormat( Qt::RichText );
	cost_fire_l->setText( "<font size = 4>Cost fire</font>" );
	cost_fire_l->setMinimumWidth( 70 );
	info_title_w_l->addWidget( cost_fire_l );

	QLabel* buy_l = new QLabel( info_title_w );
	buy_l->setTextFormat( Qt::RichText );
	buy_l->setText( "<font size = 4> </font>" );
	buy_l->setMinimumWidth( 70 );
	info_title_w_l->addWidget( buy_l );

	info_title_w->setLayout( info_title_w_l );
	layout->addWidget( info_title_w );


	for( map< string, unit* >::iterator it = prototypes::get_instance()->units.begin(); it != prototypes::get_instance()->units.end(); ++it )
	{
		unit_buy* ub = new unit_buy( it->second, this );
		units_buy_array.push_back( ub );
		layout->addWidget( ub );
	};


	this->setLayout(layout);



	set_loaded_objects();

	for( vector< unit_buy* >::iterator it = units_buy_array.begin(); it != units_buy_array.end(); ++it )
	{
			( *it )->buy_b->setEnabled( ( ( int )(*it)->u->get_price() <= current_player->get_time() ) );
	}

	set_label_money();

};



human_widget::~human_widget()
{
	delete map_array;
};



void human_widget::create_map()
{
	parameters* param = parameters::get_instance();
	game_gui* ggui = game_gui::get_instance();
	mapa* map = mapa::get_instance();
	map_units_square* mus;
	for ( unsigned int i = 0; i < mapa::get_instance()->get_size_x(); ++i )
		for ( unsigned int j = 0; j < mapa::get_instance()->get_size_y(); ++j )
		{
			//place, where units can be placed
			if ( map->terrain->get( i, j ) % 10 > 0 )
			{
				mus = new map_units_square( ( map->terrain->get( i, j ) % 10 ) - 1 );
				mus->setPos( i * param->gui_parameters[ "ICON_W" ], j * param->gui_parameters[ "ICON_H" ] );
				gs->addItem( mus );
			}
			map_array->set( new map_square_buy(), i, j );
			map_array->get( i, j )->setPixmap( *ggui->graphics->terrain_pixmaps[ ( ( map->terrain->get( i, j ) / 10 ) * 10 ) ] );
			gs->addItem( map_array->get( i, j ) );
			map_array->get( i, j )->setPos( i * param->gui_parameters[ "ICON_W" ], j * param->gui_parameters[ "ICON_H" ] );
		} 
};



void human_widget::set_label_money()
{
	ostringstream sout;
	sout << "<font size=5>Money left: " << current_player->get_time() << "</font>";
	money_l->setText( sout.str().c_str() );
};



QLabel* human_widget::print_bought_unit( unit* bu )
{
	QLabel* new_unit = new QLabel( bought_units_w );
	new_unit->setTextFormat( Qt::RichText );
	ostringstream sout;
	sout << "<font size=4>" << bu->get_name() << " " << bu->get_health() << " [" << bu->get_x() << "," << bu->get_y() << "]</font>";
	new_unit->setText( sout.str().c_str() );
	bought_units_w_l->insertWidget( ( bought_units_w_l->count() - 1 ), new_unit );
	return new_unit;
};



void human_widget::set_loaded_objects()
{
	parameters* param = parameters::get_instance();
	for ( unsigned int i = 0; i < mapa::get_instance()->get_size_x(); ++i )
		for ( unsigned int j =0; j < mapa::get_instance()->get_size_y(); ++j )
		{	
			unit* u = ( dynamic_cast< unit* >( mapa::get_instance()->get_object( i, j ) ) );
			bonus* b = ( dynamic_cast< bonus* >( mapa::get_instance()->get_object( i, j ) ) );
			if ( ( u != 0 ) && ( u->get_nbr() < 0 ) && ( u->get_player() == current_player ) )
			{
				map_unit_buy* lu = new map_unit_buy( u );
				QLabel* ret = print_bought_unit( lu->u );
				lu->setUnitInfo( ret );
				//map_units_square* mpuns = dynamic_cast< map_units_square* >( gs->itemAt( u->get_x() * param->gui_parameters[ "ICON_W" ] + 1, u->get_y() * param->gui_parameters[ "ICON_H" ] + 1 ) );
				//if ( mpuns != 0 )
				//{
				//	gs->removeItem( mpuns );
				//	delete mpuns;
				//};
				lu->setPos( u->get_x() * param->gui_parameters[ "ICON_W" ], u->get_y() * param->gui_parameters[ "ICON_H" ] );	
				gs->addItem( lu );						
			}
			else if ( b != 0 )
			{
				map_bonus_buy* mb = new map_bonus_buy( b );
				gs->addItem( mb );
				mb->setPos( b->get_x() * param->gui_parameters[ "ICON_W" ], b->get_y() * param->gui_parameters[ "ICON_H" ] );
			}
		}
};



void human_widget::show_buy( unit* u, unsigned int x, unsigned int y, pre_buy_info* pbi )
{
	map_unit_buy* mub = new map_unit_buy( u );
	QLabel* ret = print_bought_unit( u );
	mub->setPos( x * parameters::get_instance()->gui_parameters[ "ICON_W" ], y * parameters::get_instance()->gui_parameters[ "ICON_H" ] );
	mub->setUnitInfo( ret );
	gs->addItem( mub );
	set_label_money();
	bought_unit = -1;
	for( vector< unit_buy* >::iterator it = units_buy_array.begin(); it != units_buy_array.end(); ++it )
	{
		( *it )->buy_b->setEnabled( ( ( int )(*it)->u->get_price() <= current_player->get_time() ) );
	}
};



void human_widget::show_sell( unit* u, pre_sell_info* psi )
{
	unsigned int ICON_W = parameters::get_instance()->gui_parameters[ "ICON_W" ];
	unsigned int ICON_H = parameters::get_instance()->gui_parameters[ "ICON_H" ];
	map_unit_buy* mub = dynamic_cast< map_unit_buy* >( gs->itemAt( psi->ox * ICON_W + 1, psi->oy * ICON_H + 1 ) );
	if ( mub != 0 )
	{
		delete mub->uinfo;
		gs->removeItem( mub );
		delete mub;
	}
	for( vector< unit_buy* >::iterator it = units_buy_array.begin(); it != units_buy_array.end(); ++it )
	{
		(*it)->buy_b->setEnabled( ( ( int )(*it)->u->get_price() <= current_player->get_time() ) );
	}
	set_label_money();
};